package com.rst.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Book;
import com.rst.model.BookCategory;
import com.rst.model.IssueBook;
import com.rst.service.LibraryService;


@Controller
public class LibraryController {
	
	@Autowired 
	LibraryService libraryService;
	
	// library book category CRUD
	
	@RequestMapping(value="/createBookCategory", method=RequestMethod.GET)
	public ModelAndView addBookCategory()
	{
		ModelAndView mav = new ModelAndView("library/bookCategory/createBookCategory");
		BookCategory bookCategory = new BookCategory();
		mav.getModelMap().put("bookCategory", bookCategory);
		return mav;
	}
	
	@RequestMapping(value="/saveBookCategory", method=RequestMethod.POST)
	public String saveSection(@ModelAttribute("bookCategory")BookCategory bookCategory, BindingResult result, SessionStatus status)
	{
		libraryService.saveBookCategory(bookCategory);
		status.setComplete();
		return "redirect:manageBookCategory.htm";
	}
	
	@RequestMapping(value="/editBookCategory", method=RequestMethod.GET)
	public ModelAndView editBookCategory(@RequestParam("id")Long bookCategoryId)
	{
		ModelAndView mav = new ModelAndView("library/bookCategory/editBookCategory");
		BookCategory bookCategory = libraryService.getBookCategoryById(bookCategoryId);
		mav.addObject("bookCategory", bookCategory);
		return mav;
	}
	
	@RequestMapping(value="/manageBookCategory", method=RequestMethod.GET)
	public ModelAndView manageClass()
	{
		ModelAndView mav = new ModelAndView("library/bookCategory/manageBookCategory");
		List<BookCategory> bookCategoriesList   = libraryService.getBookCategoryList();
		mav.addObject("bookCategoriesList", bookCategoriesList);
		return mav;
	}
	@RequestMapping(value="/deleteBookCategory", method=RequestMethod.GET)
	public String deleteBookCategory(@RequestParam("id")Long bookCategoryId)
	{
		String status = libraryService.deleteBookCategory(bookCategoryId);
		return "redirect:manageBookCategory.htm" ;
	}
	@RequestMapping(value="/updateBookCategory", method=RequestMethod.POST)
	public String updateExamGroups(@ModelAttribute("bookCategory") BookCategory bookCategory, SessionStatus status)
	{
		
		libraryService.updateBookCategory(bookCategory);	
		status.setComplete();
		return "redirect:manageBookCategory.htm";
	}
	
	
	
	
		
	// library book CRUD
	
	@RequestMapping(value="/createBook", method=RequestMethod.GET)
	public ModelAndView addBook()
	{
		ModelAndView mav = new ModelAndView("library/book/createBook");
		Book book = new Book();
		List<BookCategory> bookCategoriesList   = libraryService.getBookCategoryList();
		mav.addObject("bookCategoriesList", bookCategoriesList);
		mav.getModelMap().put("book", book);
		return mav;
	}
	
	@RequestMapping(value="/saveBook", method=RequestMethod.POST)
	public String saveBook(@ModelAttribute("book")Book book, BindingResult result, SessionStatus status)
	{
		libraryService.saveBook(book);
		status.setComplete();
		return "redirect:manageBooks.htm";
	}
	
	@RequestMapping(value="/editBook", method=RequestMethod.GET)
	public ModelAndView editBook(@RequestParam("id")Long bookId)
	{
		ModelAndView mav = new ModelAndView("library/book/editBook");
		Book book = libraryService.getBookById(bookId);
		List<BookCategory> bookCategoriesList   = libraryService.getBookCategoryList();
		mav.addObject("bookCategoriesList", bookCategoriesList);
		mav.addObject("book", book);
		return mav;
	}
	
	@RequestMapping(value="/manageBooks", method=RequestMethod.GET)
	public ModelAndView manageBooks()
	{
		ModelAndView mav = new ModelAndView("library/book/manageBooks");
		List<Book> booksList   = libraryService.getBooksList();
		mav.addObject("booksList", booksList);
		return mav;
	}
	@RequestMapping(value="/deleteBook", method=RequestMethod.GET)
	public String deleteBook(@RequestParam("id")Long bookId)
	{
		String status = libraryService.deleteBook(bookId);
		return "redirect:manageBooks.htm" ;
	}
	
	@RequestMapping(value="/updateBook", method=RequestMethod.POST)
	public String updateBook(@ModelAttribute("book")Book book, SessionStatus status)
	{
		
		libraryService.updateBook(book);	
		status.setComplete();
		return "redirect:manageBooks.htm";
	}
	
	
	
	
	@RequestMapping(value="/searchBook", method=RequestMethod.GET)
	public ModelAndView searchBook(@RequestParam("id")Long bookNumber)
	{
		ModelAndView mav = new ModelAndView("library/book/searchBook");
		Book book = libraryService.getBookByBookNumber(bookNumber);
		mav.addObject("book", book);
		return mav;
	}
	
	@RequestMapping(value="/issueBook", method=RequestMethod.POST)
	public String issueBook(@ModelAttribute("bookIssue")IssueBook issueBook, BindingResult result, SessionStatus status)
	{
		libraryService.saveIssueBook(issueBook);
		status.setComplete();
		return "redirect:manageBookCategory.htm";
	}
	
	@RequestMapping(value="/returnBook", method=RequestMethod.POST)
	public String returnBook(@ModelAttribute("bookIssue")IssueBook issueBook, BindingResult result, SessionStatus status)
	{
		libraryService.saveIssueBook(issueBook);
		status.setComplete();
		return "redirect:manageBookCategory.htm";
	}
	
}
